/**
 * 2017年4月26日
 */
package util.tags;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import pojo.weichatlab.figure.LineChart;

/**
 * @author Alex
 *
 */
public class LineChartTag extends BaseTag{
	JspWriter out;
	LineChart lc = null;
	String selector;
	String dataSource;
	public void doTag() throws JspException,IOException{
		out = getJspContext().getOut();
		if(LineChart.class.isAssignableFrom(this.getAttribute(dataSource).getClass())){
			lc = LineChart.class.cast(this.getAttribute(dataSource));
			out.println("<script type=text/javascript>");
			out.println(" 			                                           ");
			out.println(" 			$('"+selector+"').highcharts({                                           ");
			out.println("	    		chart:{                                           ");
			out.println("	    			type:'line',	                                           ");
			out.println("	    		},                                           ");
			out.println("	        title: {                                           ");
			out.println("	            text: '"+lc.title+"',                                           ");
			out.println("	            x: -20 //center                                           ");
			out.println("	        },                                           ");
			out.println("	        subtitle: {                                           ");
			out.println("	            text: '"+lc.subTitle+"',                                           ");
			out.println("	            x: -20                                           ");
			out.println("	        },                                           ");
			out.println("	        xAxis: {                                           ");
			out.println("	            categories:"+toString(lc.x)+"                                           ");
			out.println("	        },                                           ");
			out.println("	        yAxis: {                                           ");
			out.println("	            title: {                                           ");
			out.println("	                text: '"+lc.sName+"'                                           ");
			out.println("	            },                                           ");
			out.println("	            plotLines: [{                                           ");
			out.println("	                value: 0,                                           ");
			out.println("	                width: 1,                                           ");
			out.println("	                color: '#808080'                                           ");
			out.println("	            }]                                           ");
			out.println("	        },                                           ");
			out.println("	        tooltip: {                                           ");
			out.println("	            valueSuffix: '"+lc.valueSuffix+"'                                           ");
			out.println("	        },                                           ");
			out.println("	        legend: {                                           ");
			out.println("	            layout: 'vertical',                                           ");
			out.println("	            align: 'right',                                           ");
			out.println("	            verticalAlign: 'middle',                                           ");
			out.println("	            borderWidth: 0                                           ");
			out.println("	        },                                           ");
			out.println("	        series: [{                                           ");
			out.println("	            name: '"+lc.sName+"',                                           ");
			out.println("	            data:"+toString(lc.y)+"                                           ");
			out.println("	        }]                                           ");
			out.println("	    });                                           ");
			out.println("			                                           ");
			out.println("</script>");
		}else{
			out.println("data type error;");
		}
		
//		this.getAttribute(attName)
//		lc.title;
//		lc.subTitle;
//		lc.valueSuffix;
//		lc.sName;
//		lc.x;
//		lc.y;
		
	
		
	}
	private String toString(String []array){
		StringBuilder buf = new StringBuilder();
	    buf.append('[');
	    int maxIndex = array.length - 1;
	    for (int i = 0; i <= maxIndex; i++) {
	    	 buf.append("'").append(array[i]).append("'");
	      if (i < maxIndex) {
	        buf.append(", ");
	      }
	    }
	    buf.append(']');
	    return buf.toString();
		
	}
	private String toString(double []array){
		StringBuilder buf = new StringBuilder();
	    buf.append('[');
	    int maxIndex = array.length - 1;
	    for (int i = 0; i <= maxIndex; i++) {
	      buf.append(array[i]);
	      if (i < maxIndex) {
	        buf.append(", ");
	      }
	    }
	    buf.append(']');
	    return buf.toString();
		
	}
	/**
	 * @param lc the lc to set
	 */
	public void setLc(LineChart lc) {
		this.lc = lc;
	}
	/**
	 * @param selector the selector to set
	 */
	public void setSelector(String selector) {
		this.selector = selector;
	}
	/**
	 * @param dataSource the dataSource to set
	 */
	public void setDataSource(String dataSource) {
		this.dataSource = dataSource;
	}

	
	
}
